package xyz.labmem.main.modular.app.jtys.controller;

import com.alibaba.excel.EasyExcel
import xyz.labmem.main.modular.common.service.selectFormPage
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import xyz.labmem.base.core.annotion.BusinessLog
import xyz.labmem.base.core.tool.isNotBlank
import xyz.labmem.main.modular.common.pojo.FormPage
import xyz.labmem.base.core.base.BaseVerify
import xyz.labmem.base.core.common.request.ComDelBatchReq
import xyz.labmem.base.core.common.response.ComAndColumnRes
import xyz.labmem.base.core.common.response.ComPageAndColumnRes
import xyz.labmem.base.core.common.response.ResData
import xyz.labmem.base.core.entity.ModuleVo
import xyz.labmem.base.core.entity.TableList
import xyz.labmem.base.core.enums.LogAnnotationOpTypeEnum
import xyz.labmem.base.core.result.RP
import xyz.labmem.base.core.result.RV
import xyz.labmem.base.sys.base.BaseController
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.main.modular.app.jtys.entity.Hywlgscjb
import xyz.labmem.main.modular.app.jtys.mapper.Hywlgscjb_Mapper
import xyz.labmem.main.modular.common.pojo.FormMap
import xyz.labmem.main.modular.common.service.appDownloadTemplate
import xyz.labmem.main.modular.common.service.AppTableExcelListener
import javax.servlet.http.HttpServletResponse

/**
 * 航运（物流）公司采集Controller
 * 
 * @author ruoyi
 * @date 2022-02-24
 */

@RestController
@Api(tags = ["航运（物流）公司"])
@RequestMapping("${PathPrefixConst.APP_PATH_PREFIX}hywlgscjb")
class Hywlgscjb_Controller (
    private var hywlgscjbMapper: Hywlgscjb_Mapper
) : BaseController() {

    @ApiOperation(value = "获取详情")
    @GetMapping(value = ["/{id}"])
    fun getInfo(@PathVariable("id") id: String): RV<*> {
        return success(ResData<Hywlgscjb>().apply {
            data = hywlgscjbMapper.selectById(id)
            list = getFields2(Hywlgscjb::class)
        })
    }

    /**
     * 查询航运（物流）公司采集分页展示
     */
    @PostMapping("page")
    @ApiOperation(value = "分页展示航运（物流）公司采集")
    fun page(@RequestBody @Validated(BaseVerify.page::class) req: FormPage): RP<ComPageAndColumnRes<Hywlgscjb>> {
        val pages =  hywlgscjbMapper.selectPage(
            req.IPage(),
            selectFormPage<Hywlgscjb>(req.query)
        )
        return pageWithColumn(pages, Hywlgscjb::class.java)
    }

    @PostMapping("mapDots")
    @ApiOperation(value = "地图打点航运（物流）公司采集")
    fun mapDots(@RequestBody @Validated(BaseVerify.page::class) req: FormMap): RP<ComAndColumnRes<Hywlgscjb>> {
        val pages =hywlgscjbMapper.selectList(selectFormPage<Hywlgscjb>(req.query));
        return pageWithColumn(pages, Hywlgscjb::class.java)
    }

    @PostMapping("count")
    @ApiOperation(value = "统计展示航运（物流）公司")
    fun count(@RequestBody @Validated(BaseVerify.query::class) req: FormPage): RV<TableList<ModuleVo>> {
        val arrays = arrayOf("yycbqks_dl","yycbqks_xl","jyfw_dl","jyfw_xl")
        val arrayns = arrayOf("拥有船舶_大类","拥有船舶_小类","经营范围_大类","经营范围_小类")
        var lists = ArrayList<ArrayList<ModuleVo>>()
        for(array in arrays){
            var array = hywlgscjbMapper.selectCountByTable(array)
            lists.add(array)
        }
        return success(lists,arrays,arrayns)
    }

    @GetMapping("getFields")
    @ApiOperation(value = "获取所有字段航运（物流）公司采集")
    fun getFields(): RV<ArrayList<xyz.labmem.base.core.common.response.PageColumn>> {
        return success(getFields(Hywlgscjb::class))
    }
    @BusinessLog(title = "修改数据航运（物流）公司采集", opType = LogAnnotationOpTypeEnum.UPDATE)
    @PostMapping("update")
    @ApiOperation(value = "修改数据航运（物流）公司采集")
    fun update(@RequestBody req: Hywlgscjb): RV<*> {
        return if (req.id.isNotBlank()) {
            resultJudgment(req.updateById())
        } else
            failedMsg("id不能为空")
    }
    @BusinessLog(title = "批量删除航运（物流）公司采集", opType = LogAnnotationOpTypeEnum.DELETE)
    @DeleteMapping("delBatch")
    @ApiOperation(value = "批量删除航运（物流）公司采集")
    fun delBatch(@RequestBody @Validated req: ComDelBatchReq): RV<*> {
        return resultJudgment(hywlgscjbMapper.deleteBatchIds(req.ids), "删除成功！", "没有数据被删除")
    }

    @BusinessLog(title = "导入航运（物流）公司采集", opType = LogAnnotationOpTypeEnum.IMPORT)
    @PostMapping("importData")
    @ApiOperation(value = "导入数据,“excel格式按照模板填写!”")
    fun importData(@RequestPart("file") file: MultipartFile): RV<*> {
        EasyExcel.read(
            file.inputStream,
            Hywlgscjb::class.java,
            AppTableExcelListener(hywlgscjbMapper)
        ).sheet().doRead()
        return success()
    }

    @GetMapping("downloadTemplate")
    @ApiOperation(value = "下载导入模板")
    fun downloadTemplate(response: HttpServletResponse) {
        appDownloadTemplate(this, response, Hywlgscjb(),hywlgscjbMapper)
    }
}
